package com.selfmade.transferutil.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.selfmade.transferutil.R;
import com.selfmade.transferutil.model.Book;

import java.util.List;

/**
 * Created by Selfmade on 2017/7/25/0025.
 */

public class BookListAdapter extends ArrayAdapter<Book> {

    private List<Book> objects;
    private Context context;


    public BookListAdapter(Context context, int resourceId, List<Book> objects) {
        super(context, resourceId, objects);
        this.objects = objects;
        this.context = context;

    }

    private static class ViewHolder {
        TextView bookID;
        TextView bookName, bookAuthor;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Book getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView==null) {
            viewHolder=new ViewHolder();
            LayoutInflater mInflater=LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.book_item, null);
            viewHolder.bookID = (TextView) convertView.findViewById(R.id.book_id);
            viewHolder.bookName = (TextView) convertView.findViewById(R.id.book_name);
            viewHolder.bookAuthor = (TextView) convertView.findViewById(R.id.book_author);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Book book = objects.get(position);
        if(null!=book) {
            viewHolder.bookID.setText(book.getID());
            viewHolder.bookName.setText(book.getName());
            viewHolder.bookAuthor.setText(book.getAuthor());
        }

        return convertView;
    }

}